/*
 * This class displays the lesson into a pdf format, saves it, and then opens it
 * for user viewing.
 */

/**
 *
 * @author link3988
 */

import topics.SubTopic;
import topics.MainTopic;
import topics.MicroTopic;
import java.util.ArrayList;
import com.gnostice.pdfone.*;
import com.gnostice.pdfone.fonts.PdfFont;
import com.gnostice.pdfone.encodings.PdfEncodings;

public class SequentialLesson {

        /**
         * Constructor for this class.
         * activates pdf license.
         * @param topics
         */
        public SequentialLesson(ArrayList<MainTopic> topics)
        {

                PDFOne.activate("U8QVT9:6VH06VQ:H12SPEV:GQ6O8", "8ETGRE20:XRZ4R4R5E:BERXB7IRD:4XN5010");
                createPDF(topics);
        }

        /**
         * Creates the different fonts for the pdf, then
         * Runs through the data structure and prints to the pdf.
         * Then it saves it and opens it.
         * @param lessn
         */
        public void createPDF(ArrayList<MainTopic> lessn)
        {
            try
            {
                PdfWriter save = PdfWriter.fileWriter("lesson.pdf");
                PdfDocument document = new PdfDocument(save);

                PdfFont mainTopicFont = PdfFont.create(
                    "Arial",    // name of installed font
                    PdfFont.BOLD | PdfFont.UNDERLINE | PdfFont.STROKE_AND_FILL,
            	    40, PdfEncodings.WINANSI);

                PdfFont subTopicFont = PdfFont.create(
                    "Arial",    // name of installed font
                    PdfFont.STROKE_AND_FILL,
            	    26, PdfEncodings.WINANSI);

                PdfFont microTopicFont = PdfFont.create(
                    "Times New Roman",    // name of installed font
                    PdfFont.ITALIC | PdfFont.STROKE_AND_FILL,
            	    18, PdfEncodings.WINANSI);

            int height = 0;
            int width = 0;

            for(int i = 0; i < lessn.size(); i++)
            {
                MainTopic mt = lessn.get(i);
                //Write main topic
                document.writeText(mt.getTopic(),
                mainTopicFont,      // font
                0, height);
                height += 45;
                if(height > 800)
                        height = 0;
                for(int j = 0; j < mt.subSize(); j ++)
                {
                    SubTopic st = mt.getSubTopic(j);
                    //Write subtopics here
                    document.writeText("- " + st.getTopic(),
                    subTopicFont,      // font
                    60, height);
                    height += 31;
                    if(height > 800)
                        height = 0;
                    for(int k = 0; k < st.microSize(); k++)
                    {
                         MicroTopic micro = st.getMicroTopic(k);
                         //Write micro topics
                         document.writeText("+ " + micro.getTopic(),
                         microTopicFont,      // font
                         120, height);
                         height += 23;
                         if(height > 800)
                            height = 0;
                    }
                }

            }
            document.setOpenAfterSave(true);

            // Write to output file
            document.write();

            // Close all I/O streams associated with the PDF reader
            save.dispose();

            Runtime.getRuntime().exec( "cmd /c start lesson.pdf");


            }catch(Exception exc){System.out.println("IO exception.");}


        }
}
